package com.creating.mict.action;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.creating.mict.domain.User;
import com.creating.mict.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {

	@Resource
	private IUserService userService;
	
	
	/**
	* @Title: checkLoginName 
	* @Description: 校验新增账户是否已存在
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String checkLoginName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		User user = userService.getUserByLoginName(loginName);
		if(user != null) {
			request.setAttribute("result", "1"); //账号已存在
		} else {  
			request.setAttribute("result", "0"); //账号可以使用
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: queryUserList 
	* @Description: 获取用户列表
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String queryUserList() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//或当前登录用户名
		String loginOffice = (String)request.getSession().getAttribute("loginOffice");
		String loginType = (String)request.getSession().getAttribute("loginType");
		
		if(loginType.equals("1")) {
			
		} else {
			foffice = loginOffice;
		}
		
		List<User> list = userService.queryUserList(foffice);
		if(list == null) {
			total = 0;
		} else {
			total = list.size();
		}
		StringBuffer jsonStr = new StringBuffer("[");
		for (int i = 0; i < total; i++) {
			User user = (User) list.get(i);
			jsonStr.append("{");
			jsonStr.append("loginName:'").append(user.getLoginName()).append("',");
			jsonStr.append("password:'").append(user.getPassword()).append("',");
			jsonStr.append("fname:'").append(user.getFname()).append("',");
			jsonStr.append("ftype:'").append(user.getFtype()).append("',");
			jsonStr.append("fphone:'").append(user.getFphone()).append("',");
			jsonStr.append("fldate:'").append(user.getLastDateStr()).append("'");
			if (i == total - 1) {
				jsonStr.append("}");
			} else {
				jsonStr.append("},");
			}
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + total + ", root:"
				+ jsonStr.toString() + "}";
		request.setAttribute("result", str);
		return "success";
	}
	
	/**
	* @Title: addUser 
	* @Description: 新增用户
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String addUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//或当前登录用户名
		String loginOffice = (String)request.getSession().getAttribute("loginOffice");
		String loginType = (String)request.getSession().getAttribute("loginType");
		
		User user = userService.getUserByLoginName(loginName);
		if(user == null) {
			user = new User();
			user.setLoginName(loginName);
			if(loginType.equals("1")) {
				user.setFoffice(foffice);
				user.setFtype("2");
			} else {
				user.setFoffice(loginOffice);
				user.setFtype("3");
			}
			user.setCreatDate(new Date());
			user.setFstate("1");
		} else {
			
		}
		
		user.setPassword(password);
		user.setFname(fname);
		user.setFphone(fphone);
		
		boolean boo1 = userService.saveUser(user);
		if(boo1) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: modPassword 
	* @Description: 修改密码
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String modPassword() {
		HttpServletRequest request = ServletActionContext.getRequest();
		loginName = (String)request.getSession().getAttribute("loginName");
		User user = userService.getUserByLoginName(loginName);
		if(user != null && password2.equals(user.getPassword())) {
			user.setPassword(password);
			userService.saveUser(user);
			request.setAttribute("result", "{success:true,message:'ok'}");
		} else {  
			request.setAttribute("result", "{success:false,message:'error'}");
		}
		return "success";
	}
	
	/**
	* @Title: deleteRole 
	* @Description: 根据删除单个角色
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String deleteUserByLoginName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(userService.deleteUserById(loginName)) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		}else {  //失败
			request.setAttribute("result", "{success:false,message:'失败！'}");	
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: saveUserRole 
	* @Description: 保存用户角色
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String saveUserRole() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = userService.saveUserRole(loginName, roleId);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'保存成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'保存失败!'}");
		}
		return "success";
	}
	
	/**
	 * 
	* @Title: clearRoleColumns 
	* @Description: 清空角色对应栏目
	* @param @return 
	* @return String    返回类型 
	* @throws
	 */
	public String clearUserRole() {
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean boo = userService.clearUserRole(loginName);
		if(boo) {
			request.setAttribute("result", "{success:true,message:'成功!'}");
		} else {
			request.setAttribute("result", "{success:true,message:'失败!'}");
		}
		return "success";
	}
	
	
	
	private int limit = 50;
	private int start = 0;
	private int total;
	private String loginName;
	private String password;
	private String ftype; //类型
	private String fldate;//上次登录日期
	private String roleId;
	private String fname;
	private String foffice;
	private String fphone;

	private String password1;
	private String password2;

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getFoffice() {
		return foffice;
	}

	public void setFoffice(String foffice) {
		this.foffice = foffice;
	}

	public String getFname() {
		return fname;
	}

	public void setFname(String fname) {
		this.fname = fname;
	}

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}
	/**
	 * @param limit the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}
	/**
	 * @return the start
	 */
	public int getStart() {
		return start;
	}
	/**
	 * @param start the start to set
	 */
	public void setStart(int start) {
		this.start = start;
	}
	/**
	 * @return the total
	 */
	public int getTotal() {
		return total;
	}
	/**
	 * @param total the total to set
	 */
	public void setTotal(int total) {
		this.total = total;
	}
	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}
	/**
	 * @param loginName the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the ftype
	 */
	public String getFtype() {
		return ftype;
	}
	/**
	 * @param ftype the ftype to set
	 */
	public void setFtype(String ftype) {
		this.ftype = ftype;
	}
	/**
	 * @return the fldate
	 */
	public String getFldate() {
		return fldate;
	}
	/**
	 * @param fldate the fldate to set
	 */
	public void setFldate(String fldate) {
		this.fldate = fldate;
	}

	/**
	 * @return the roleId
	 */
	public String getRoleId() {
		return roleId;
	}

	/**
	 * @param roleId the roleId to set
	 */
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getFphone() {
		return fphone;
	}

	public void setFphone(String fphone) {
		this.fphone = fphone;
	}
}
